home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 April / enter-2004-04.iso / files / httrack-3.30.exe / {app} / src / htswizard.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-10-11  |  33.0 KB  |  931 lines

  1. /* ------------------------------------------------------------ */
  2. /*
  3. HTTrack Website Copier, Offline Browser for Windows and Unix
  4. Copyright (C) Xavier Roche and other contributors
  5.  
  6. This program is free software; you can redistribute it and/or
  7. modify it under the terms of the GNU General Public License
  8. as published by the Free Software Foundation; either version 2
  9. of the License, or any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  19.  
  20.  
  21. Important notes:
  22.  
  23. - We hereby ask people using this source NOT to use it in purpose of grabbing
  24. emails addresses, or collecting any other private information on persons.
  25. This would disgrace our work, and spoil the many hours we spent on it.
  26.  
  27.  
  28. Please visit our Website: http://www.httrack.com
  29. */
  30.  
  31.  
  32. /* ------------------------------------------------------------ */
  33. /* File: httrack.c subroutines:                                 */
  34. /*       wizard system (accept/refuse links)                    */
  35. /* Author: Xavier Roche                                         */
  36. /* ------------------------------------------------------------ */
  37.  
  38. #include "htswizard.h"
  39. #include "htsdefines.h"
  40.  
  41. /* specific definitions */
  42. #include "htsbase.h"
  43. #include <stdio.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <ctype.h>
  47. /* END specific definitions */
  48.  
  49. // version 1 pour httpmirror
  50. // flusher si on doit lire peu α peu le fichier
  51. #define test_flush if (opt->flush) { fflush(opt->log); fflush(opt->errlog); }
  52.  
  53. // pour allΘger la syntaxe, des raccourcis sont crΘΘs
  54. #define urladr   (liens[ptr]->adr)
  55. #define urlfil   (liens[ptr]->fil)
  56.  
  57. // libΘrer filters[0] pour insΘrer un ΘlΘment dans filters[0]
  58. #define HT_INSERT_FILTERS0 do {\
  59.   int i;\
  60.   if (*opt->filters.filptr > 0) {\
  61.     for(i = (*opt->filters.filptr)-1 ; i>=0 ; i--) {\
  62.       strcpybuff((*opt->filters.filters)[i+1],(*opt->filters.filters)[i]);\
  63.     }\
  64.   }\
  65.   (*opt->filters.filters)[0][0]='\0';\
  66.   (*opt->filters.filptr)++;\
  67.   assertf((*opt->filters.filptr) < opt->maxfilter); \
  68. } while(0)
  69.  
  70.  
  71.  
  72. /*
  73. httrackp opt     bloc d'options
  74. int ptr,int lien_tot,lien_url** liens
  75.                              relatif aux liens
  76. char* adr,char* fil
  77.                              adresse/fichier α tester
  78. char** filters,int filptr,int filter_max
  79.                              relatif aux filtres
  80. robots_wizard* robots
  81.                              relatif aux robots
  82. int* set_prio_to
  83.                              callback obligatoire "capturer ce lien avec prio=N-1"
  84. int* just_test_it
  85.                              callback optionnel "ne faire que tester ce lien Θventuellement"
  86. retour:
  87.                0 acceptΘ
  88.                1 refusΘ
  89.                -1 pas d'avis
  90. */
  91. int hts_acceptlink(httrackp* opt,
  92.                    int ptr,int lien_tot,lien_url** liens,
  93.                    char* adr,char* fil,
  94.                    int* set_prio_to,
  95.                    int* just_test_it) {
  96.   
  97.   int forbidden_url=-1;
  98.   int meme_adresse;
  99. #define _FILTERS     (*opt->filters.filters)
  100. #define _FILTERS_PTR (opt->filters.filptr)
  101. #define _ROBOTS      ((robots_wizard*)opt->robotsptr)
  102.   int may_set_prio_to=0;
  103.  
  104.   // -------------------- PHASE 0 --------------------
  105.  
  106.   /* Infos */
  107.   if ((opt->debug>1) && (opt->log!=NULL)) {
  108.     fspc(opt->log,"debug"); fprintf(opt->log,"wizard test begins: %s%s"LF,adr,fil);
  109.     test_flush;
  110.   }
  111.   
  112.   /* Already exists? Then, we know that we knew that this link had to be known */
  113.   if (adr[0] != '\0'
  114.     && fil[0] != '\0'
  115.     && opt->hash != NULL
  116.     && hash_read((hash_struct*)opt->hash, adr, fil, 1, opt->urlhack) >= 0
  117.     ) {
  118.     return 0;  /* Yokai */
  119.   }
  120.   
  121.   // -------------------- PHASE 1 --------------------
  122.  
  123.   /* Doit-on traiter les non html? */
  124.   if ((opt->getmode & 2)==0) {    // non on ne doit pas
  125.     if (!ishtml(fil)) {  // non il ne faut pas
  126.       //adr[0]='\0';    // ne pas traiter ce lien, pas traiter
  127.       forbidden_url=1;    // interdire rΘcupΘration du lien
  128.       if ((opt->debug>1) && (opt->log!=NULL)) {
  129.         fspc(opt->log,"debug"); fprintf(opt->log,"non-html file ignored at %s : %s"LF,adr,fil);
  130.         test_flush;
  131.       }
  132.       
  133.     }
  134.   }
  135.   
  136.   /* Niveau 1: ne pas parser suivant! */
  137.   if (ptr>0) {
  138.     if (liens[ptr]->depth <= 1) {
  139.       forbidden_url=1;    // interdire rΘcupΘration du lien
  140.       if ((opt->debug>1) && (opt->log!=NULL)) {
  141.         fspc(opt->log,"debug"); fprintf(opt->log,"file from too far level ignored at %s : %s"LF,adr,fil);
  142.         test_flush;
  143.       }
  144.     }
  145.   }
  146.  
  147.   /* en cas d'Θchec en phase 1, retour immΘdiat! */
  148.   if (forbidden_url==1) {
  149.     return forbidden_url;
  150.   }
  151.   
  152.   // -------------------- PHASE 2 --------------------
  153.  
  154.   // ------------------------------------------------------
  155.   // doit-on traiter ce lien?.. vΘrifier droits de dΘplacement
  156.   meme_adresse=strfield2(adr,urladr);
  157.   if ((opt->debug>1) && (opt->log!=NULL)) {
  158.     fspc(opt->log,"debug"); 
  159.     if (meme_adresse) 
  160.       fprintf(opt->log,"Compare addresses: %s=%s"LF,adr,urladr);
  161.     else
  162.       fprintf(opt->log,"Compare addresses: %s!=%s"LF,adr,urladr);
  163.     test_flush;
  164.   }
  165.   if (meme_adresse) {  // mΩme adresse 
  166.     {  // tester interdiction de descendre
  167.       // MODIFIE : en cas de remontΘe puis de redescente, il se pouvait qu'on ne puisse pas atteindre certains fichiers
  168.       // problΦme: si un fichier est virtuellement accessible via une page mais dont le lien est sur une autre *uniquement*..
  169.       char tempo[HTS_URLMAXSIZE*2];
  170.       char tempo2[HTS_URLMAXSIZE*2];
  171.       tempo[0] = tempo2[0] = '\0';
  172.       
  173.       // note (up/down): on calcule α partir du lien primaire, ET du lien prΘcΘdent.
  174.       // ex: si on descend 2 fois on peut remonter 1 fois
  175.       
  176.       if (lienrelatif(tempo,fil,liens[liens[ptr]->premier]->fil)==0) {
  177.         if (lienrelatif(tempo2,fil,liens[ptr]->fil)==0) {
  178.           if ((opt->debug>1) && (opt->log!=NULL)) {
  179.             fspc(opt->log,"debug"); fprintf(opt->log,"build relative links to test: %s %s (with %s and %s)"LF,tempo,tempo2,liens[liens[ptr]->premier]->fil,liens[ptr]->fil);
  180.             test_flush;
  181.           }
  182.           
  183.           // si vient de primary, ne pas tester lienrelatif avec (car host "diffΘrent")
  184.           /*if (liens[liens[ptr]->premier] == 0) {   // vient de primary
  185.           }
  186.           */
  187.           
  188.           // NEW: finalement OK, sauf pour les moved repΘrΘs par link_import
  189.           // PROBLEME : annulΘ a cause d'un lien Θventuel isolΘ acceptΘ..qui entrainerait un miroir
  190.           
  191.           // (test mΩme niveau (NOUVEAU α cause de certains problΦmes de filtres non intΘgrΘs))
  192.           // NEW
  193.           if (tempo[0] != '\0' && tempo[1] != '\0') {
  194.             if ( (!strchr(tempo+1,'/')) || (!strchr(tempo2+1,'/')) ) {
  195.               if (!liens[ptr]->link_import) {   // ne rΘsulte pas d'un 'moved'
  196.                 forbidden_url=0;
  197.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  198.                   fspc(opt->log,"debug"); fprintf(opt->log,"same level link authorized: %s%s"LF,adr,fil);
  199.                   test_flush;
  200.                 }
  201.               }
  202.             }
  203.           }
  204.           
  205.           // down
  206.           if ( (strncmp(tempo,"../",3)) || (strncmp(tempo2,"../",3)))  {   // pas montΘe sinon ne nbous concerne pas
  207.             int test1,test2;
  208.             if (!strncmp(tempo,"../",3))
  209.               test1=0;
  210.             else
  211.               test1 = (strchr(tempo +((*tempo =='/')?1:0),'/')!=NULL);
  212.             if (!strncmp(tempo2,"../",3))
  213.               test2=0;
  214.             else
  215.               test2 = (strchr(tempo2+((*tempo2=='/')?1:0),'/')!=NULL);
  216.             if ( (test1) && (test2) ) {   // on ne peut que descendre
  217.               if ((opt->seeker & 1)==0) {  // interdiction de descendre
  218.                 forbidden_url=1;
  219.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  220.                   fspc(opt->log,"debug"); fprintf(opt->log,"lower link canceled: %s%s"LF,adr,fil);
  221.                   test_flush;
  222.                 }
  223.               } else {    // autorisΘ α priori - NEW
  224.                 if (!liens[ptr]->link_import) {   // ne rΘsulte pas d'un 'moved'
  225.                   forbidden_url=0;
  226.                   if ((opt->debug>1) && (opt->log!=NULL)) {
  227.                     fspc(opt->log,"debug"); fprintf(opt->log,"lower link authorized: %s%s"LF,adr,fil);
  228.                     test_flush;
  229.                   }
  230.                 }
  231.               }
  232.             } else if ( (test1) || (test2) ) {   // on peut descendre pour accΘder au lien
  233.               if ((opt->seeker & 1)!=0) {  // on peut descendre - NEW
  234.                 if (!liens[ptr]->link_import) {   // ne rΘsulte pas d'un 'moved'
  235.                   forbidden_url=0;
  236.                   if ((opt->debug>1) && (opt->log!=NULL)) {
  237.                     fspc(opt->log,"debug"); fprintf(opt->log,"lower link authorized: %s%s"LF,adr,fil);
  238.                     test_flush;
  239.                   }
  240.                 }
  241.               }
  242.             }
  243.           }
  244.           
  245.           
  246.           // up
  247.           if ( (!strncmp(tempo,"../",3)) && (!strncmp(tempo2,"../",3)) ) {    // impossible sans monter
  248.             if ((opt->seeker & 2)==0) {  // interdiction de monter
  249.               forbidden_url=1;
  250.               if ((opt->debug>1) && (opt->log!=NULL)) {
  251.                 fspc(opt->log,"debug"); fprintf(opt->log,"upper link canceled: %s%s"LF,adr,fil);
  252.                 test_flush;
  253.               }
  254.             } else {       // autorisΘ α monter - NEW
  255.               if (!liens[ptr]->link_import) {   // ne rΘsulte pas d'un 'moved'
  256.                 forbidden_url=0;
  257.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  258.                   fspc(opt->log,"debug"); fprintf(opt->log,"upper link authorized: %s%s"LF,adr,fil);
  259.                   test_flush;
  260.                 }
  261.               }
  262.             }
  263.           } else if ( (!strncmp(tempo,"../",3)) || (!strncmp(tempo2,"../",3)) ) {    // Possible en montant
  264.             if ((opt->seeker & 2)!=0) {  // autorisΘ α monter - NEW
  265.               if (!liens[ptr]->link_import) {   // ne rΘsulte pas d'un 'moved'
  266.                 forbidden_url=0;
  267.                 if ((opt->debug>1) && (opt->log!=NULL)) {
  268.                   fspc(opt->log,"debug"); fprintf(opt->log,"upper link authorized: %s%s"LF,adr,fil);
  269.                   test_flush;
  270.                 }
  271.               }
  272.             }  // sinon autorisΘ en descente
  273.           }
  274.           
  275.           
  276.         } else {
  277.           if (opt->errlog) {
  278.             fprintf(opt->errlog,"Error building relative link %s and %s"LF,fil,liens[ptr]->fil);
  279.             test_flush;
  280.           }
  281.         }
  282.       } else {
  283.         if (opt->errlog) {
  284.           fprintf(opt->errlog,"Error building relative link %s and %s"LF,fil,liens[liens[ptr]->premier]->fil);
  285.           test_flush;
  286.         }
  287.       }
  288.       
  289.     }  // tester interdiction de descendre?
  290.     
  291.     {  // tester interdiction de monter
  292.       char tempo[HTS_URLMAXSIZE*2];
  293.       char tempo2[HTS_URLMAXSIZE*2];
  294.       if (lienrelatif(tempo,fil,liens[liens[ptr]->premier]->fil)==0) {
  295.         if (lienrelatif(tempo2,fil,liens[ptr]->fil)==0) {
  296.         } else {
  297.           if (opt->errlog) { 
  298.             fprintf(opt->errlog,"Error building relative link %s and %s"LF,fil,liens[ptr]->fil);
  299.             test_flush;
  300.           }
  301.           
  302.         }
  303.       } else {
  304.         if (opt->errlog) { 
  305.           fprintf(opt->errlog,"Error building relative link %s and %s"LF,fil,liens[liens[ptr]->premier]->fil);
  306.           test_flush;
  307.         }
  308.         
  309.       }
  310.     }   // fin tester interdiction de monter
  311.     
  312.   } else {    // adresse diffΘrente, sortir?
  313.     
  314.     //if (!opt->wizard) {    // mode non wizard
  315.     // doit-on traiter ce lien?.. vΘrifier droits de sortie
  316.     switch((opt->travel & 255)) {
  317.     case 0: 
  318.       if (!opt->wizard)    // mode non wizard
  319.         forbidden_url=1; break;    // interdicton de sortir au dela de l'adresse
  320.     case 1: {              // sortie sur le mΩme dom.xxx
  321.       int i=strlen(adr)-1;
  322.       int j=strlen(urladr)-1;
  323.       while( (i>0) && (adr[i]!='.')) i--;
  324.       while( (j>0) && (urladr[j]!='.')) j--;
  325.       i--; j--;
  326.       while( (i>0) && (adr[i]!='.')) i--;
  327.       while( (j>0) && (urladr[j]!='.')) j--;
  328.       if ((i>0) && (j>0)) {
  329.         if (!strfield2(adr+i,urladr+j)) {   // !=
  330.           if (!opt->wizard) {   // mode non wizard
  331.             //printf("refused: %s\n",adr);
  332.             forbidden_url=1;  // pas mΩme domaine  
  333.             if ((opt->debug>1) && (opt->log!=NULL)) {
  334.               fspc(opt->log,"debug"); fprintf(opt->log,"foreign domain link canceled: %s%s"LF,adr,fil);
  335.               test_flush;
  336.             }
  337.           }
  338.           
  339.         } else {
  340.           if (opt->wizard) {   // mode wizard
  341.             forbidden_url=0;  // mΩme domaine  
  342.             if ((opt->debug>1) && (opt->log!=NULL)) {
  343.               fspc(opt->log,"debug"); fprintf(opt->log,"same domain link authorized: %s%s"LF,adr,fil);
  344.               test_flush;
  345.             }
  346.           }
  347.         }
  348.         
  349.       } else
  350.         forbidden_url=1;
  351.             } 
  352.       break;  
  353.     case 2: {                      // sortie sur le mΩme .xxx
  354.       int i=strlen(adr)-1;
  355.       int j=strlen(urladr)-1;
  356.       while( (i>0) && (adr[i]!='.')) i--;
  357.       while( (j>0) && (urladr[j]!='.')) j--;
  358.       if ((i>0) && (j>0)) {
  359.         if (!strfield2(adr+i,urladr+j)) {   // !-
  360.           if (!opt->wizard) {   // mode non wizard
  361.             //printf("refused: %s\n",adr);
  362.             forbidden_url=1;  // pas mΩme .xx  
  363.             if ((opt->debug>1) && (opt->log!=NULL)) {
  364.               fspc(opt->log,"debug"); fprintf(opt->log,"foreign location link canceled: %s%s"LF,adr,fil);
  365.               test_flush;
  366.             }
  367.           }
  368.         } else {
  369.           if (opt->wizard) {   // mode wizard
  370.             forbidden_url=0;  // mΩme domaine  
  371.             if ((opt->debug>1) && (opt->log!=NULL)) {
  372.               fspc(opt->log,"debug"); fprintf(opt->log,"same location link authorized: %s%s"LF,adr,fil);
  373.               test_flush;
  374.             }
  375.           }
  376.         }
  377.       } else forbidden_url=1;     
  378.             } 
  379.       break;
  380.     case 7:                 // everywhere!!
  381.       if (opt->wizard) {   // mode wizard
  382.         forbidden_url=0;
  383.         break;
  384.       }
  385.     }  // switch
  386.     
  387.     // ANCIENNE POS -- rΘcupΘrer les liens α c⌠tΘs d'un lien (nearlink)
  388.     
  389.   }  // fin test adresse identique/diffΘrente
  390.  
  391.   // -------------------- PHASE 3 --------------------
  392.  
  393.   // rΘcupΘrer les liens α c⌠tΘs d'un lien (nearlink) (nvelle pos)
  394.   if (opt->nearlink) {
  395.     if (!ishtml(fil)) {  // non html
  396.       //printf("ok %s%s\n",ad,fil);
  397.       forbidden_url=0;    // autoriser
  398.       may_set_prio_to=1+1; // set prio to 1 (parse but skip urls) if near is the winner
  399.       if ((opt->debug>1) && (opt->log!=NULL)) {
  400.         fspc(opt->log,"debug"); fprintf(opt->log,"near link authorized: %s%s"LF,adr,fil);
  401.         test_flush;
  402.       }
  403.     }
  404.   }
  405.   
  406.   // -------------------- PHASE 4 --------------------
  407.   
  408.   // ------------------------------------------------------
  409.   // Si wizard, il se peut qu'on autorise ou qu'on interdise 
  410.   // un lien spΘcial avant mΩme de tester sa position, sa hiΘrarchie etc.
  411.   // peut court-circuiter le forbidden_url prΘcΘdent
  412.   if (opt->wizard) { // le wizard entre en action..
  413.     //
  414.     int question=1;         // poser une question                            
  415.     int force_mirror=0;     // pour mirror links
  416.     int filters_answer=0;   // dΘcision prise par les filtres
  417.     char l[HTS_URLMAXSIZE*2];
  418.     char lfull[HTS_URLMAXSIZE*2];
  419.     
  420.     if (forbidden_url!=-1) question=0;  // pas de question, rΘsolu
  421.     
  422.     // former URL complΦte du lien actuel
  423.     strcpybuff(l,jump_identification(adr));
  424.     if (*fil!='/') strcatbuff(l,"/");
  425.     strcatbuff(l,fil);
  426.     // full version (http://foo:bar@www.foo.com/bar.html)
  427.     if (!link_has_authority(adr))
  428.       strcpybuff(lfull,"http://");
  429.     else
  430.       lfull[0]='\0';
  431.     strcatbuff(lfull,adr);
  432.     if (*fil!='/') strcatbuff(lfull,"/");
  433.     strcatbuff(lfull,fil);
  434.     
  435.     // tester filters (URLs autorisΘes ou interdites explicitement)
  436.     
  437.     // si lien primaire on saute le joker, on est pas lΘmur
  438.     if (ptr==0) {  // lien primaire, autoriser
  439.       question=1;    // la question sera rΘsolue automatiquement
  440.       forbidden_url=0;
  441.       may_set_prio_to=0;    // clear may-set flag
  442.     } else {
  443.       // eternal depth first
  444.       // vΘrifier rΘcursivitΘ extΘrieure
  445.       if (opt->extdepth>0) {
  446.         if ( /*question && */ (ptr>0) && (!force_mirror)) {
  447.           // well, this is kinda a hak
  448.           // we don't want to mirror EVERYTHING, and we have to decide where to stop
  449.           // there is no way yet to tag "external" links, and therefore links that are
  450.           // "weak" (authorized depth < external depth) are just not considered for external
  451.           // hack
  452.           if (liens[ptr]->depth > opt->extdepth) {
  453.             // *set_prio_to = opt->extdepth + 1;
  454.             *set_prio_to = 1 + (opt->extdepth);
  455.             may_set_prio_to=0;  // clear may-set flag
  456.             forbidden_url=0;    // autorisΘ
  457.             question=0;         // rΘsolution auto
  458.             if ((opt->debug>1) && (opt->log!=NULL)) {
  459.               if (question) {
  460.                 fspc(opt->log,"debug"); fprintf(opt->log,"(wizard) ambiguous link accepted (external depth): link %s at %s%s"LF,l,urladr,urlfil);
  461.               } else {
  462.                 fspc(opt->log,"debug"); fprintf(opt->log,"(wizard) forced to accept link (external depth): link %s at %s%s"LF,l,urladr,urlfil);
  463.               }
  464.               test_flush;
  465.             }
  466.             
  467.           }
  468.         }
  469.       }  
  470.       
  471.       // filters
  472.       {
  473.         int jok;
  474.         char* mdepth="";
  475.         // filters, 0=sait pas 1=ok -1=interdit
  476.         {
  477.           int jokDepth1=0,jokDepth2=0;
  478.           int jok1=0,jok2=0;
  479.           jok1  = fa_strjoker(_FILTERS,*_FILTERS_PTR,lfull,NULL,NULL,&jokDepth1);
  480.           jok2 =  fa_strjoker(_FILTERS,*_FILTERS_PTR,l,    NULL,NULL,&jokDepth2);
  481.           if (jok2 == 0) {      // #2 doesn't know
  482.             jok = jok1;        // then, use #1
  483.             mdepth = _FILTERS[jokDepth1];
  484.           } else if (jok1 == 0) { // #1 doesn't know
  485.             jok = jok2;        // then, use #2
  486.             mdepth = _FILTERS[jokDepth2];
  487.           } else if (jokDepth1 >= jokDepth2) { // #1 matching rule is "after" #2, then it is prioritary
  488.             jok = jok1;
  489.             mdepth = _FILTERS[jokDepth1];
  490.           } else {                             // #2 matching rule is "after" #1, then it is prioritary
  491.             jok = jok2;
  492.             mdepth = _FILTERS[jokDepth2];
  493.           }
  494.         }
  495.         
  496.         if (jok == 1) {   // autorisΘ
  497.           filters_answer=1;  // dΘcision prise par les filtres
  498.           question=0;    // ne pas poser de question, autorisΘ
  499.           forbidden_url=0;  // URL autorisΘe
  500.           may_set_prio_to=0;    // clear may-set flag
  501.           if ((opt->debug>1) && (opt->log!=NULL)) {
  502.             fspc(opt->log,"debug"); fprintf(opt->log,"(wizard) explicit authorized (%s) link: link %s at %s%s"LF,mdepth,l,urladr,urlfil);
  503.             test_flush;
  504.           }
  505.         } else if (jok == -1) {  // forbidden
  506.           filters_answer=1;  // dΘcision prise par les filtres
  507.           question=0;    // ne pas poser de question:
  508.           forbidden_url=1;   // URL interdite
  509.           if ((opt->debug>1) && (opt->log!=NULL)) {
  510.             fspc(opt->log,"debug"); fprintf(opt->log,"(wizard) explicit forbidden (%s) link: link %s at %s%s"LF,mdepth,l,urladr,urlfil);
  511.             test_flush;
  512.           }
  513.         }  // sinon on touche α rien
  514.       }
  515.     }
  516.     
  517.     // vΘrifier mode mirror links
  518.     if (question) {
  519.       if (opt->mirror_first_page) {    // mode mirror links
  520.         if (liens[ptr]->precedent==0) {  // parent=primary!
  521.           forbidden_url=0;    // autorisΘ
  522.           may_set_prio_to=0;    // clear may-set flag
  523.           question=1;         // rΘsolution auto
  524.           force_mirror=5;     // mirror (5)
  525.           if ((opt->debug>1) && (opt->log!=NULL)) {
  526.             fspc(opt->log,"debug"); fprintf(opt->log,"(wizard) explicit mirror link: link %s at %s%s"LF,l,urladr,urlfil);
  527.             test_flush;
  528.           }
  529.         }
  530.       }
  531.     }
  532.     
  533.     // on doit poser la question.. peut on la poser?
  534.     // (oui je sais quel preuve de dΘlicatesse, merci merci)      
  535.     if ((question) && (ptr>0) && (!force_mirror)) {
  536.       if (opt->wizard==2) {    // Θliminer tous les liens non rΘpertoriΘs comme autorisΘs (ou inconnus)
  537.         question=0;
  538.         forbidden_url=1;
  539.         if ((opt->debug>1) && (opt->log!=NULL)) {
  540.           fspc(opt->log,"debug"); fprintf(opt->log,"(wizard) ambiguous forbidden link: link %s at %s%s"LF,l,urladr,urlfil);
  541.           test_flush;
  542.         }
  543.       }
  544.     }
  545.     
  546.     // vΘrifier robots.txt
  547.     if (opt->robots) {
  548.       int r = checkrobots(_ROBOTS,adr,fil);
  549.       if (r == -1) {    // interdiction
  550. #if DEBUG_ROBOTS
  551.         printf("robots.txt forbidden: %s%s\n",adr,fil);
  552. #endif
  553.         // question rΘsolue, par les filtres, et mode robot non strict
  554.         if ((!question) && (filters_answer) && (opt->robots == 1) && (forbidden_url!=1)) {
  555.           r=0;    // annuler interdiction des robots
  556.           if (!forbidden_url) {
  557.             if ((opt->debug>1) && (opt->log!=NULL)) {
  558.               fspc(opt->log,"debug"); fprintf(opt->log,"Warning link followed against robots.txt: link %s at %s%s"LF,l,adr,fil);
  559.               test_flush;
  560.             }
  561.           }
  562.         }
  563.         if (r == -1) {    // interdire
  564.           forbidden_url=1;
  565.           question=0;
  566.           if ((opt->debug>1) && (opt->log!=NULL)) {
  567.             fspc(opt->log,"debug"); fprintf(opt->log,"(robots.txt) forbidden link: link %s at %s%s"LF,l,adr,fil);
  568.             test_flush;
  569.           }
  570.         }
  571.       }
  572.     }
  573.     
  574.     if (!question) {
  575.       if ((opt->debug>1) && (opt->log!=NULL)) {
  576.         if (!forbidden_url) {
  577.           fspc(opt->log,"debug"); fprintf(opt->log,"(wizard) shared foreign domain link: link %s at %s%s"LF,l,urladr,urlfil);
  578.         } else {
  579.           fspc(opt->log,"debug"); fprintf(opt->log,"(wizard) cancelled foreign domain link: link %s at %s%s"LF,l,urladr,urlfil);
  580.         }
  581.         test_flush;
  582.       }
  583. #if BDEBUG==3
  584.       printf("at %s in %s, wizard says: url %s ",urladr,urlfil,l);
  585.       if (forbidden_url) printf("cancelled"); else printf(">SHARED<");
  586.       printf("\n");
  587. #endif 
  588.     }
  589.  
  590.     /* en cas de question, ou lien primaire (enregistrer autorisations) */
  591.     if (question || (ptr==0)) {
  592. #if HTS_ANALYSTE
  593.       char* s;
  594. #else
  595.       char s[4];
  596. #endif
  597.       int n=0;
  598.       
  599.       // si primaire (plus bas) alors ...
  600.       if ((ptr!=0) && (force_mirror==0)) {
  601.         HTS_REQUEST_START;
  602.         HT_PRINT("\n");
  603.         HT_PRINT("At "); HT_PRINT(urladr); HT_PRINT(", there is a link ("); HT_PRINT(adr); HT_PRINT("/"); HT_PRINT(fil); HT_PRINT(") which goes outside the address."LF);
  604.         HT_PRINT("What should I do? (press a key + enter)"LF LF);
  605.         HT_PRINT("* Ignore all further links" LF);
  606.         HT_PRINT("0 Ignore this link (default if empty entry)"LF);
  607.         HT_PRINT("1 Ignore directory and lower structures"LF);
  608.         HT_PRINT("2 Ignore all domain"LF);
  609.         //HT_PRINT("3 (Ignore location, not implemented)\n");
  610.         HT_PRINT(LF);
  611.         HT_PRINT("4 Get only this page/link"LF);
  612.         HT_PRINT("5 Mirror this link (useful)"LF);
  613.         HT_PRINT("6 Mirror links located in the same domain"LF);
  614.         HT_PRINT(LF);
  615. //#if HTS_ANALYSTE!=2
  616. //HT_PRINT("! View extract of html code where the link is located"LF);
  617. //#endif
  618.         HTS_REQUEST_END;
  619. #if HTS_ANALYSTE
  620.           {
  621.             char tempo[HTS_URLMAXSIZE*2];
  622.             tempo[0]='\0';
  623.             strcatbuff(tempo,adr);
  624.             strcatbuff(tempo,"/");
  625.             strcatbuff(tempo,fil);
  626.             s=hts_htmlcheck_query3(tempo);
  627.           }
  628. #else
  629.         do {
  630.           io_flush; linput(stdin,s,2);
  631. #endif
  632.           if (strnotempty(s)==0)  // entrΘe
  633.             n=0;
  634.           else if (isdigit((unsigned char)*s))
  635.             sscanf(s,"%d",&n);
  636.           else {
  637.             switch(*s) {
  638.             case '*': n=-1; break;
  639.             case '!': n=-999; {
  640.               /*char *a;
  641.               int i;                                    
  642.               a=copie_de_adr-128;
  643.               if (a<r.adr) a=r.adr;
  644.               for(i=0;i<256;i++) {
  645.                 if (a==copie_de_adr) printf("\nHERE:\n");
  646.                 printf("%c",*a++);
  647.               }
  648.               printf("\n\n");
  649.               */
  650.                       }
  651.               break;
  652.             default: n=-999; printf("What did you say?\n"); break;
  653.               
  654.             } 
  655.           }
  656. #if HTS_ANALYSTE
  657. #else
  658.         } while(n==-999);
  659. #endif
  660.         io_flush;
  661.       } else {   // lien primaire: autoriser rΘpertoire entier       
  662.         if (!force_mirror) {
  663.           if ((opt->seeker & 1)==0) {  // interdiction de descendre
  664.             n=7;
  665.           } else {
  666.             n=5;   // autoriser miroir rΘpertoires descendants (lien primaire)
  667.           }
  668.         } else   // forcer valeur (sub-wizard)
  669.           n=force_mirror;
  670.       }
  671.       
  672.       /* sanity check - reallocate filters HERE */
  673.       if ((*_FILTERS_PTR) + 1 >= opt->maxfilter) {
  674.         opt->maxfilter += HTS_FILTERSINC;
  675.         if (filters_init(&_FILTERS, opt->maxfilter, HTS_FILTERSINC) == 0) {
  676.           printf("PANIC! : Too many filters : >%d [%d]\n", (*_FILTERS_PTR),__LINE__);
  677.           fflush(stdout);
  678.           if (opt->errlog) {
  679.             fprintf(opt->errlog,LF"Too many filters, giving up..(>%d)"LF, (*_FILTERS_PTR) );
  680.             fprintf(opt->errlog,"To avoid that: use #F option for more filters (example: -#F5000)"LF);
  681.             test_flush;
  682.           }
  683.           assertf("too many filters - giving up" == NULL);    // wild..
  684.         }
  685.       }
  686.  
  687.       // here we have enough room for a new filter if necessary
  688.       switch(n) {
  689.       case -1: // sauter tout le reste
  690.         forbidden_url=1;
  691.         opt->wizard=2;    // sauter tout le reste
  692.         break;
  693.       case 0:    // interdire les mΩmes liens: adr/fil
  694.         forbidden_url=1; 
  695.         HT_INSERT_FILTERS0;    // insΘrer en 0
  696.         strcpybuff(_FILTERS[0],"-");
  697.         strcatbuff(_FILTERS[0],jump_identification(adr));
  698.         if (*fil!='/') strcatbuff(_FILTERS[0],"/");
  699.         strcatbuff(_FILTERS[0],fil);
  700.         break;
  701.         
  702.       case 1: // Θliminer rΘpertoire entier et sous rΘp: adr/path/ *
  703.         forbidden_url=1;
  704.         {
  705.           int i=strlen(fil)-1;
  706.           while((fil[i]!='/') && (i>0)) i--;
  707.           if (fil[i]=='/') {
  708.             HT_INSERT_FILTERS0;    // insΘrer en 0
  709.             strcpybuff(_FILTERS[0],"-");
  710.             strcatbuff(_FILTERS[0],jump_identification(adr));
  711.             if (*fil!='/') strcatbuff(_FILTERS[0],"/");
  712.             strncatbuff(_FILTERS[0] ,fil,i);
  713.             if (_FILTERS[0][strlen(_FILTERS[0])-1]!='/') 
  714.               strcatbuff(_FILTERS[0],"/");
  715.             strcatbuff(_FILTERS[0],"*");
  716.           }
  717.         }            
  718.         
  719.         // ** ...
  720.         break;
  721.         
  722.       case 2:    // adresse adr*
  723.         forbidden_url=1;
  724.         HT_INSERT_FILTERS0;    // insΘrer en 0                                
  725.         strcpybuff(_FILTERS[0],"-");
  726.         strcatbuff(_FILTERS[0],jump_identification(adr));
  727.         strcatbuff(_FILTERS[0],"*");
  728.         break;
  729.         
  730.       case 3: // ** A FAIRE
  731.         forbidden_url=1;
  732.         /*
  733.         {
  734.         int i=strlen(adr)-1;
  735.         while((adr[i]!='/') && (i>0)) i--;
  736.         if (i>0) {
  737.         
  738.           }
  739.           
  740.       }*/
  741.         
  742.         break;
  743.         //
  744.       case 4:    // same link
  745.         // PAS BESOIN!!
  746.         /*HT_INSERT_FILTERS0;    // insΘrer en 0                                
  747.         strcpybuff(_FILTERS[0],"+");
  748.         strcatbuff(_FILTERS[0],adr);
  749.         if (*fil!='/') strcatbuff(_FILTERS[0],"/");
  750.         strcatbuff(_FILTERS[0],fil);*/
  751.         
  752.         
  753.         // Θtant donnΘ le renversement wizard/primary filter (les primary autorisent up/down ET interdisent)
  754.         // il faut Θviter d'un lien isolΘ effectue un miroir total..
  755.         
  756.         *set_prio_to = 0+1;    // niveau de rΘcursion=0 (pas de miroir)
  757.         
  758.         break;
  759.         
  760.       case 5:    // autoriser rΘpertoire entier et fils
  761.         if ((opt->seeker & 2)==0) {  // interdiction de monter
  762.           int i=strlen(fil)-1;
  763.           while((fil[i]!='/') && (i>0)) i--;
  764.           if (fil[i]=='/') {
  765.             HT_INSERT_FILTERS0;    // insΘrer en 0                                
  766.             strcpybuff(_FILTERS[0],"+");
  767.             strcatbuff(_FILTERS[0],jump_identification(adr));
  768.             if (*fil!='/') strcatbuff(_FILTERS[0],"/");
  769.             strncatbuff(_FILTERS[0],fil,i+1);
  770.             strcatbuff(_FILTERS[0],"*");
  771.           }
  772.         } else {    // autoriser domaine alors!!
  773.           HT_INSERT_FILTERS0;    // insΘrer en 0                                strcpybuff(filters[filptr],"+");
  774.           strcpybuff(_FILTERS[0],"+");
  775.           strcatbuff(_FILTERS[0],jump_identification(adr));
  776.           strcatbuff(_FILTERS[0],"*");
  777.         }
  778.         break;
  779.         
  780.       case 6:    // same domain
  781.         HT_INSERT_FILTERS0;    // insΘrer en 0                                strcpybuff(filters[filptr],"+");
  782.         strcpybuff(_FILTERS[0],"+");
  783.         strcatbuff(_FILTERS[0],jump_identification(adr));
  784.         strcatbuff(_FILTERS[0],"*");
  785.         break;
  786.         //
  787.       case 7:    // autoriser ce rΘpertoire
  788.         {
  789.           int i=strlen(fil)-1;
  790.           while((fil[i]!='/') && (i>0)) i--;
  791.           if (fil[i]=='/') {
  792.             HT_INSERT_FILTERS0;    // insΘrer en 0                                
  793.             strcpybuff(_FILTERS[0],"+");
  794.             strcatbuff(_FILTERS[0],jump_identification(adr));
  795.             if (*fil!='/') strcatbuff(_FILTERS[0],"/");
  796.             strncatbuff(_FILTERS[0],fil,i+1);
  797.             strcatbuff(_FILTERS[0],"*[file]");
  798.           }
  799.         }
  800.         
  801.         break;
  802.         
  803.       case 50:    // on fait rien
  804.         break;
  805.       }  // switch 
  806.                               
  807.     }  // test du wizard sur l'url
  808.   }  // fin du test wizard..
  809.  
  810.   // -------------------- PHASE 5 --------------------
  811.  
  812.   // lien non autorisΘ, peut-on juste le tester?
  813.   if (just_test_it) {
  814.     if (forbidden_url==1) {
  815.       if (opt->travel&256) {    // tester tout de mΩme
  816.         if (strfield(adr,"ftp://")==0) {    // PAS ftp!
  817.           forbidden_url=1;    // oui oui toujours interdit (note: sert α rien car ==1 mais c pour comprendre)
  818.           *just_test_it=1;     // mais on teste
  819.           if ((opt->debug>1) && (opt->log!=NULL)) {
  820.             fspc(opt->log,"debug"); fprintf(opt->log,"Testing link %s%s"LF,adr,fil);
  821.           }
  822.         }
  823.       }
  824.     }
  825.     //adr[0]='\0';  // cancel
  826.   }
  827.  
  828.   // -------------------- PHASE 6 --------------------
  829. #if HTS_ANALYSTE
  830.   {
  831.     int test_url=hts_htmlcheck_check(adr,fil,forbidden_url);
  832.     if (test_url!=-1) {
  833.       forbidden_url=test_url;
  834.       may_set_prio_to=0;    // clear may-set flag
  835.     }
  836.   }
  837. #endif
  838.  
  839.   // -------------------- FINAL PHASE --------------------
  840.   // Test if the "Near" test won
  841.   if (may_set_prio_to && forbidden_url == 0) {
  842.     *set_prio_to = may_set_prio_to;
  843.   }
  844.  
  845.   return forbidden_url;
  846. }
  847.  
  848. // tester taille
  849. int hts_testlinksize(httrackp* opt,
  850.                      char* adr,char* fil,
  851.                      LLint size) {
  852.   int jok=0;
  853.   if (size>=0) {
  854.     char l[HTS_URLMAXSIZE*2];
  855.     char lfull[HTS_URLMAXSIZE*2];
  856.     if (size>=0) {
  857.       LLint sz=size;
  858.       int size_flag=0;
  859.       
  860.       // former URL complΦte du lien actuel
  861.       strcpybuff(l,jump_identification(adr));
  862.       if (*fil!='/') strcatbuff(l,"/");
  863.       strcatbuff(l,fil);
  864.       //
  865.       if (!link_has_authority(adr))
  866.         strcpybuff(lfull,"http://");
  867.       else
  868.         lfull[0]='\0';
  869.       strcatbuff(lfull,adr);
  870.       if (*fil!='/') strcatbuff(l,"/");
  871.       strcatbuff(lfull,fil);
  872.       
  873.       // tester filtres (taille)
  874.       // jok = fa_strjoker(opt->filters.filters,*opt->filters.filptr,l,&sz,&size_flag,NULL);
  875.  
  876.       // filters, 0=sait pas 1=ok -1=interdit
  877.       {
  878.         int jokDepth1=0,jokDepth2=0;
  879.         int jok1=0,jok2=0;
  880.         LLint sz1=size,sz2=size;
  881.         int size_flag1=0,size_flag2=0;
  882.         jok1  = fa_strjoker(*opt->filters.filters,*opt->filters.filptr,lfull,&sz1,&size_flag1,&jokDepth1);
  883.         jok2 =  fa_strjoker(*opt->filters.filters,*opt->filters.filptr,l,    &sz2,&size_flag2,&jokDepth2);
  884.         if (jok2 == 0) {      // #2 doesn't know
  885.           jok = jok1;        // then, use #1
  886.           sz = sz1;
  887.           size_flag = size_flag1;
  888.         } else if (jok1 == 0) {  // #1 doesn't know
  889.           jok = jok2;        // then, use #2
  890.           sz = sz2;
  891.           size_flag = size_flag2;
  892.         } else if (jokDepth1 >= jokDepth2) { // #1 matching rule is "after" #2, then it is prioritary
  893.           jok = jok1;
  894.           sz = sz1;
  895.           size_flag = size_flag1;
  896.         } else {                              // #2 matching rule is "after" #1, then it is prioritary
  897.           jok = jok2;
  898.           sz = sz2;
  899.           size_flag = size_flag2;
  900.         } 
  901.       }
  902.       
  903.  
  904.       // log
  905.       if (jok==1) {
  906.         if ((opt->debug>1) && (opt->log!=NULL)) {
  907.           fspc(opt->log,"debug"); fprintf(opt->log,"File confirmed (size test): %s%s ("LLintP")"LF,adr,fil,(LLint)(size));
  908.         }
  909.       } else if (jok==-1) {
  910.         if (size_flag) {        /* interdit α cause de la taille */
  911.           if ((opt->debug>1) && (opt->log!=NULL)) {
  912.             fspc(opt->log,"debug"); fprintf(opt->log,"File cancelled due to its size: %s%s ("LLintP", limit: "LLintP")"LF,adr,fil,(LLint)(size),(LLint)(sz));
  913.           }
  914.         } else {
  915.           jok=1;
  916.         }
  917.       }
  918.     }
  919.   }
  920.   return jok;
  921. }
  922.  
  923.  
  924.  
  925. #undef test_flush
  926. #undef urladr
  927. #undef urlfil
  928.  
  929. #undef HT_INSERT_FILTERS0
  930.  
  931.